# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import fieldMap_Chen


class IndustryDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertIndus(self, info):
        """
        添加 industry 信息
        :param info:
        :return:
        """
        indusName = info['indusName']
        indusPcode = info['indusPcode']
        indusCode = info['indusCode']
        indusIsLock = info['indusIsLock']
        indusSort = info['indusSort']

        sql = "insert into zzh_industry (industry_name, industry_pcode, industry_code,industry_is_lock,industry_sort) VALUES (%s, %s, %s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [indusName, indusPcode, indusCode, indusIsLock, indusSort])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findIndusById(self, indusId):
        """
        根据ID查询 industry 信息
        :param indusId:
        :return:
        """
        sql = "select * from zzh_industry WHERE industry_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [indusId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.industry_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findIndusByCondition(self, **kwargs):
        """
        industry 表的条件查询
        :param kwargs:
        :return:
        """

        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Chen.indestryFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_industry WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_industry WHERE " + whereSentence
        rs = cur.execute(sql)

        jsons = None
        if rs:
            rsSet = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex, pageSize, rsSet)
            jsons = Table2Json.industry_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons, rs

    def deleteIndus(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_industry WHERE industry_id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateIndus(self, info):
        """
        更新 industry 表
        :param info:
        :return:
        """
        indusId = info['indusId']
        indusName = info['indusName']
        indusPcode = info['indusPcode']
        indusCode = info['indusCode']
        indusIsLock = info['indusIsLock']
        indusSort = info['indusSort']

        sql = "update zzh_industry set industry_name=%s,industry_pcode=%s,industry_code=%s,industry_is_lock=%s,industry_sort=%s" \
              " WHERE industry_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [indusName, indusPcode, indusCode, indusIsLock, indusSort, indusId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectIndus(self, indusId):
        """
        根据ID查询，返回元组
        :param indusId:
        :return:
        """
        sql = "select * from zzh_industry WHERE industry_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [indusId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr
